<?php

/**
 * CategoryController.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysblog\Controller;

use System\Common\SystemController;

class CategoryController extends SystemController {

    public function _initialize()
    {
    
        $this->obj_serv = D('Category', 'Service');
    }
    
    public function index()
    {
        $list = $this->obj_serv->getCatList('node_id,node_name,order_sort,parent_id');
        
        $this->__proCatUrl($list);
        $list = $this->obj_serv->proCatList($list);
        
        $this->system_title = '分类设置';
        $this->assign('remote', U('Sysblog/Category/add'));
        $this->assign('data', $list);
        $this->loadSystemView();
    }

    public function add()
    {
        $cat_list = $this->obj_serv->getCatList('node_id,node_name', array(), true);
        $this->assign('catlist', $cat_list);
        $this->assign('act_url', U('Sysblog/Category/dosubmit'));
        $this->display('remote');
    }

    public function edit()
    {
        $node_id = I('get.cid');
        
        if(!$node_id)
        {
            return $this->error('不合法的参数', '' ,true);
        }
        // 获取分类的信息
        $node_info = $this->obj_serv->getCatRow($node_id, 'node_id,node_name,order_sort,parent_id');
        if(!$node_info)
        {
            return $this->error('此分类不存在', '' ,true);
        }
        $cat_list = $this->obj_serv->getCatList('node_id,node_name', array(), true);
        
        $this->assign('data', $node_info);
        $this->assign('catlist', $cat_list);
        $this->assign('act_url', U('Sysblog/Category/dosubmit'));
        $this->display('remote');
    }

    public function del()
    {
        $node_id = I('get.cid');
        
        try {
            if(!$node_id)
            {
                throw new \Exception('参数不合法');
            }
            
            $rs = $this->obj_serv->delRow($node_id);
            if(!$rs)
            {
                throw new \Exception(L('_ERR_DEL_'));
            }
        } catch (\Exception $e) {
            $msg = $e->getMessage();
            return $this->error($msg);
        }
        
        return $this->success('删除成功', '', true);
    }

    public function dosubmit()
    {
        try
        {
            $rs = $this->obj_serv->saveData(I('post.'));
            if(!$rs)
            {
                throw new \Exception('保存失败');
            }
            
        } catch ( \Exception $e )
        {
            return $this->error($e->getMessage());
        }
        
        $jumpUrl = U('Category/index');
        return $this->success('保存成功', $jumpUrl);
    }
    
    private function __proCatUrl(&$data)
    {
    
        if (! $data)
        {return array();}
    
        foreach ($data as &$v)
        {
            $v ['edit_url'] = U('Category/edit', array(
                    'cid' => $v ['node_id']
            ));
            $v ['del_url'] = U('Category/del', array(
                    'cid' => $v ['node_id']
            ));
        }
    
        return true;
    }


}